home *** CD-ROM | disk | FTP | other *** search
/ Digital Background Bonanza / Digital Background Bonanza - Disc 1.iso / pc / DBB1.swf / scripts / __Packages / mx / video / SMILManager.as < prev    next >
Encoding:
Text File  |  2007-03-09  |  9.6 KB  |  285 lines

  1. class mx.video.SMILManager
  2. {
  3.    var _owner;
  4.    var _url;
  5.    var xml;
  6.    var baseURLAttr;
  7.    var videoTags;
  8.    var width;
  9.    var height;
  10.    static var version = "1.0.1.10";
  11.    static var shortVersion = "1.0.1";
  12.    static var ELEMENT_NODE = 1;
  13.    function SMILManager(owner)
  14.    {
  15.       this._owner = owner;
  16.    }
  17.    function connectXML(url)
  18.    {
  19.       this._url = this.fixURL(url);
  20.       this.xml = new XML();
  21.       this.xml.onLoad = mx.utils.Delegate.create(this,this.xmlOnLoad);
  22.       this.xml.load(this._url);
  23.       return false;
  24.    }
  25.    function fixURL(origURL)
  26.    {
  27.       if(origURL.substr(0,5).toLowerCase() == "http:" || origURL.substr(0,6).toLowerCase() == "https:")
  28.       {
  29.          var _loc2_ = origURL.indexOf("?") < 0 ? "?" : "&";
  30.          return origURL + _loc2_ + "FLVPlaybackVersion=" + mx.video.SMILManager.shortVersion;
  31.       }
  32.       return origURL;
  33.    }
  34.    function xmlOnLoad(success)
  35.    {
  36.       try
  37.       {
  38.          if(!success)
  39.          {
  40.             this._owner.helperDone(this,false);
  41.          }
  42.          else
  43.          {
  44.             this.baseURLAttr = new Array();
  45.             this.videoTags = new Array();
  46.             var _loc2_ = this.xml.firstChild;
  47.             var _loc6_ = false;
  48.             while(_loc2_ != null)
  49.             {
  50.                if(_loc2_.nodeType == mx.video.SMILManager.ELEMENT_NODE)
  51.                {
  52.                   _loc6_ = true;
  53.                   if(_loc2_.nodeName.toLowerCase() == "smil")
  54.                   {
  55.                      break;
  56.                   }
  57.                }
  58.                _loc2_ = _loc2_.nextSibling;
  59.             }
  60.             if(!_loc6_)
  61.             {
  62.                throw new mx.video.VideoError(mx.video.VideoError.INVALID_XML,"URL: \"" + this._url + "\" No root node found; if url is for an flv it must have .flv extension and take no parameters");
  63.             }
  64.             if(_loc2_ == null)
  65.             {
  66.                throw new mx.video.VideoError(mx.video.VideoError.INVALID_XML,"URL: \"" + this._url + "\" Root node not smil");
  67.             }
  68.             var _loc5_ = false;
  69.             var _loc4_ = 0;
  70.             while(_loc4_ < _loc2_.childNodes.length)
  71.             {
  72.                var _loc3_ = _loc2_.childNodes[_loc4_];
  73.                if(_loc3_.nodeType == mx.video.SMILManager.ELEMENT_NODE)
  74.                {
  75.                   if(_loc3_.nodeName.toLowerCase() == "head")
  76.                   {
  77.                      this.parseHead(_loc3_);
  78.                   }
  79.                   else
  80.                   {
  81.                      if(_loc3_.nodeName.toLowerCase() != "body")
  82.                      {
  83.                         throw new mx.video.VideoError(mx.video.VideoError.INVALID_XML,"URL: \"" + this._url + "\" Tag " + _loc3_.nodeName + " not supported in " + _loc2_.nodeName + " tag.");
  84.                      }
  85.                      _loc5_ = true;
  86.                      this.parseBody(_loc3_);
  87.                   }
  88.                }
  89.                _loc4_ = _loc4_ + 1;
  90.             }
  91.             if(!_loc5_)
  92.             {
  93.                throw new mx.video.VideoError(mx.video.VideoError.INVALID_XML,"URL: \"" + this._url + "\" Tag body is required.");
  94.             }
  95.             this._owner.helperDone(this,true);
  96.          }
  97.       }
  98.       catch(err:Error)
  99.       {
  100.          this._owner.helperDone(this,false);
  101.          throw err;
  102.       }
  103.    }
  104.    function parseHead(parentNode)
  105.    {
  106.       var _loc4_ = false;
  107.       var _loc3_ = 0;
  108.       while(_loc3_ < parentNode.childNodes.length)
  109.       {
  110.          var _loc2_ = parentNode.childNodes[_loc3_];
  111.          if(_loc2_.nodeType == mx.video.SMILManager.ELEMENT_NODE)
  112.          {
  113.             if(_loc2_.nodeName.toLowerCase() == "meta")
  114.             {
  115.                for(var _loc6_ in _loc2_.attributes)
  116.                {
  117.                   if(_loc6_.toLowerCase() != "base")
  118.                   {
  119.                      throw new mx.video.VideoError(mx.video.VideoError.INVALID_XML,"URL: \"" + this._url + "\" Attribute " + _loc6_ + " not supported in " + _loc2_.nodeName + " tag.");
  120.                   }
  121.                   this.baseURLAttr.push(_loc2_.attributes[_loc6_]);
  122.                }
  123.             }
  124.             else if(_loc2_.nodeName.toLowerCase() == "layout")
  125.             {
  126.                if(!_loc4_)
  127.                {
  128.                   this.parseLayout(_loc2_);
  129.                   _loc4_ = true;
  130.                }
  131.             }
  132.          }
  133.          _loc3_ = _loc3_ + 1;
  134.       }
  135.    }
  136.    function parseLayout(parentNode)
  137.    {
  138.       var _loc3_ = 0;
  139.       while(_loc3_ < parentNode.childNodes.length)
  140.       {
  141.          var _loc2_ = parentNode.childNodes[_loc3_];
  142.          if(_loc2_.nodeType == mx.video.SMILManager.ELEMENT_NODE)
  143.          {
  144.             if(_loc2_.nodeName.toLowerCase() == "root-layout")
  145.             {
  146.                for(var _loc5_ in _loc2_.attributes)
  147.                {
  148.                   if(_loc5_.toLowerCase() == "width")
  149.                   {
  150.                      this.width = Number(_loc2_.attributes[_loc5_]);
  151.                   }
  152.                   else if(_loc5_.toLowerCase() == "height")
  153.                   {
  154.                      this.height = Number(_loc2_.attributes[_loc5_]);
  155.                   }
  156.                }
  157.                if(isNaN(this.width) || this.width < 0 || isNaN(this.height) || this.height < 0)
  158.                {
  159.                   throw new mx.video.VideoError(mx.video.VideoError.INVALID_XML,"URL: \"" + this._url + "\" Tag " + _loc2_.nodeName + " requires attributes id, width and height.  Width and height must be numbers greater than or equal to 0.");
  160.                }
  161.                this.width = Math.round(this.width);
  162.                this.height = Math.round(this.height);
  163.                return undefined;
  164.             }
  165.          }
  166.          _loc3_ = _loc3_ + 1;
  167.       }
  168.    }
  169.    function parseBody(parentNode)
  170.    {
  171.       var _loc6_ = 0;
  172.       var _loc3_ = 0;
  173.       while(_loc3_ < parentNode.childNodes.length)
  174.       {
  175.          var _loc2_ = parentNode.childNodes[_loc3_];
  176.          if(_loc2_.nodeType == mx.video.SMILManager.ELEMENT_NODE)
  177.          {
  178.             if((_loc6_ = _loc6_ + 1) > 1)
  179.             {
  180.                throw new mx.video.VideoError(mx.video.VideoError.INVALID_XML,"URL: \"" + this._url + "\" Tag " + parentNode.nodeName + " is required to contain exactly one tag.");
  181.             }
  182.             if(_loc2_.nodeName.toLowerCase() == "switch")
  183.             {
  184.                this.parseSwitch(_loc2_);
  185.             }
  186.             else if(_loc2_.nodeName.toLowerCase() == "video" || _loc2_.nodeName.toLowerCase() == "ref")
  187.             {
  188.                var _loc5_ = this.parseVideo(_loc2_);
  189.                this.videoTags.push(_loc5_);
  190.             }
  191.          }
  192.          _loc3_ = _loc3_ + 1;
  193.       }
  194.       if(this.videoTags.length < 1)
  195.       {
  196.          throw new mx.video.VideoError(mx.video.VideoError.INVALID_XML,"URL: \"" + this._url + "\" At least one video of ref tag is required.");
  197.       }
  198.    }
  199.    function parseSwitch(parentNode)
  200.    {
  201.       var _loc4_ = 0;
  202.       while(_loc4_ < parentNode.childNodes.length)
  203.       {
  204.          var _loc5_ = parentNode.childNodes[_loc4_];
  205.          if(_loc5_.nodeType == mx.video.SMILManager.ELEMENT_NODE)
  206.          {
  207.             if(_loc5_.nodeName.toLowerCase() == "video" || _loc5_.nodeName.toLowerCase() == "ref")
  208.             {
  209.                var _loc3_ = this.parseVideo(_loc5_);
  210.                if(_loc3_.bitrate == undefined)
  211.                {
  212.                   this.videoTags.push(_loc3_);
  213.                }
  214.                else
  215.                {
  216.                   var _loc6_ = false;
  217.                   var _loc2_ = 0;
  218.                   while(_loc2_ < this.videoTags.length)
  219.                   {
  220.                      if(this.videoTags[_loc2_].bitrate == undefined || _loc3_.bitrate < this.videoTags[_loc4_].bitrate)
  221.                      {
  222.                         _loc6_ = true;
  223.                         this.videoTags.splice(_loc2_,0,this.videoTags);
  224.                         break;
  225.                      }
  226.                      _loc2_ = _loc2_ + 1;
  227.                   }
  228.                   if(!_loc6_)
  229.                   {
  230.                      this.videoTags.push(_loc3_);
  231.                   }
  232.                }
  233.             }
  234.          }
  235.          _loc4_ = _loc4_ + 1;
  236.       }
  237.    }
  238.    function parseVideo(node)
  239.    {
  240.       var _loc3_ = new Object();
  241.       for(var _loc4_ in node.attributes)
  242.       {
  243.          if(_loc4_.toLowerCase() == "src")
  244.          {
  245.             _loc3_.src = node.attributes[_loc4_];
  246.          }
  247.          else if(_loc4_.toLowerCase() == "system-bitrate")
  248.          {
  249.             _loc3_.bitrate = Number(node.attributes[_loc4_]);
  250.          }
  251.          else if(_loc4_.toLowerCase() == "dur")
  252.          {
  253.             _loc3_.dur = this.parseTime(node.attributes[_loc4_]);
  254.          }
  255.       }
  256.       if(_loc3_.src == undefined)
  257.       {
  258.          throw new mx.video.VideoError(mx.video.VideoError.INVALID_XML,"URL: \"" + this._url + "\" Attribute src is required in " + node.nodeName + " tag.");
  259.       }
  260.       return _loc3_;
  261.    }
  262.    function parseTime(timeStr)
  263.    {
  264.       var _loc4_ = 0;
  265.       var _loc3_ = timeStr.split(":");
  266.       if(_loc3_.length < 1 || _loc3_.length > 3)
  267.       {
  268.          throw new mx.video.VideoError(mx.video.VideoError.INVALID_XML,"Invalid dur value: " + timeStr);
  269.       }
  270.       var _loc1_ = 0;
  271.       while(_loc1_ < _loc3_.length)
  272.       {
  273.          var _loc2_ = Number(_loc3_[_loc1_]);
  274.          if(isNaN(_loc2_))
  275.          {
  276.             throw new mx.video.VideoError(mx.video.VideoError.INVALID_XML,"Invalid dur value: " + timeStr);
  277.          }
  278.          _loc4_ *= 60;
  279.          _loc4_ += _loc2_;
  280.          _loc1_ = _loc1_ + 1;
  281.       }
  282.       return _loc4_;
  283.    }
  284. }
  285.